import {Editor, Toolbar} from '@wangeditor/editor-for-vue'

export const getFormData = (payload) => {
    console.log(payload, '图片路径');

    const formData = new FormData()
    for (const key in payload) {
        console.log(key, '键值对');
        formData.append(key, payload[key])
    }
    console.log(formData, '数据格式');

    return formData
}

export const getUrls = (str) => {
    const reg = /<img(?:(?!\/>).|\n)*?\/?>/gm
    const REG = /^(?:[\s\S]+)\/static\/([\s\S]+)$/
    const srcReg = /<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i
    const r1 = str.match(reg)
    if (!r1) return false
    return r1.map(item => item.match(srcReg)[1]).map(item => REG.exec(item)[1])
}

export const findDif = (arr, target) => {
    const result = []
    target.forEach(item => {
        if (!arr.includes(item)) {
            result.push(item)
        }
    })
    return result
}

export function delay(time = 300) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}

export function patchZero(value) {
    return ('0' + value).slice(-2)
}

export function getWelComTextByTime(hour, name) {
    if (hour >= 0 && hour <= 6) return `时间不早了 ${name} ，您一定有些困了，赶快休息吧！`
    if (hour > 6 && hour <= 10) return `早上好 ${name} ，开启元气满满的一天吧！`
    if (hour > 10 && hour <= 13) return `中午好 ${name} ，忙了一上午了，一定要吃午饭！`
    if (hour > 13 && hour <= 19) return `下午好 ${name} ，您一定有些累了，喝杯咖啡提提神。`
    if (hour > 19 && hour <= 22) return `晚上好 ${name} ，忙了一天了，快回家陪陪家人吧。`
    if (hour > 22) return `时间不早了 ${name} ，您一定有些困了，赶快休息吧！`
}

export function jiemaData(e) {
    // editor.getHtml()
    return decodeURIComponent(e)
}


export function copy(text) {
    return new Promise((resolve, reject) => {
        if (!navigator.clipboard) {
            return fallbackCopyTextToClipboard(text, resolve, reject);
        }
        navigator.clipboard.writeText(text).then(function () {
            resolve()
        }, function (err) {
            reject(err)
        });
    })
}

function fallbackCopyTextToClipboard(text, resolve, reject) {
    const textarea = document.createElement('textarea');
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.focus();
    textarea.select();
    try {
        const successful = document.execCommand('copy');
        if (successful) return resolve()
        reject(successful)
    } catch (err) {
        reject(err)
    }
    document.body.removeChild(textarea);
}

function binaryToStr(str){
    let result = [];
    let list = str.split(" ");
    for(let i=0;i<list.length;i++){
        let item = list[i];
        let asciiCode = parseInt(item,2);
        let charValue = String.fromCharCode(asciiCode);
        result.push(charValue);
    }
    return result.join("");
}
export function strToBinary(str){
    const result = [];
    const list = str.split("");
    for(let i=0;i<list.length;i++){
        if(i !== 0){
            result.push(" ");
        }
        let item = list[i];
        let binaryStr = item.charCodeAt().toString(2);
        result.push(binaryStr);
    }
    return result.join("");
}
